home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH7 / SRC / OBJ3POLY.CLS < prev    next >
Encoding:
Text File  |  1996-05-04  |  3.9 KB  |  153 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjPolygon"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Private num_points As Integer
  11. Private y() As Single
  12. Private x() As Single
  13.  
  14. ' ************************************************
  15. ' Return True if this polygon contains the
  16. ' indicated point.
  17. ' ************************************************
  18. Function Contains(x As Single, y As Single) As Boolean
  19. Dim xmin As Single
  20. Dim ymin As Single
  21. Dim xmax As Single
  22. Dim ymax As Single
  23.  
  24.     Bound xmin, ymin, xmax, ymax
  25.     Contains = (x >= xmin And x <= xmax And _
  26.                 y >= ymin And y <= ymax)
  27. End Function
  28.  
  29.  
  30. Function ObjectType() As String
  31.     ObjectType = "POLYGON"
  32. End Function
  33.  
  34. ' ************************************************
  35. ' Set the coordinates for a point.
  36. ' ************************************************
  37. Sub SetPoint(Index As Integer, x1 As Single, y1 As Single)
  38.     If Index < 1 Or Index > num_points Then Exit Sub
  39.     x(Index) = x1
  40.     y(Index) = y1
  41. End Sub
  42.  
  43. ' ************************************************
  44. ' Apply a transformation matrix to the object.
  45. ' ************************************************
  46. Sub Transform(M() As Single)
  47. Dim i As Integer
  48.  
  49.     For i = 1 To num_points
  50.         m2PointMultiply x(i), y(i), M
  51.     Next i
  52. End Sub
  53.  
  54. ' ************************************************
  55. ' Apply a nonlinear transformation.
  56. ' ************************************************
  57. Sub Distort(d As Object)
  58. Dim i As Integer
  59.  
  60.     For i = 1 To num_points
  61.         d.Distort x(i), y(i)
  62.     Next i
  63. End Sub
  64. ' ************************************************
  65. ' Set the number of points and redimension the
  66. ' point arrays.
  67. ' ************************************************
  68. Property Let NumPoints(n As Integer)
  69.     If n < 1 Then
  70.         Erase x
  71.         Erase y
  72.         num_points = 0
  73.         Exit Property
  74.     End If
  75.     
  76.     num_points = n
  77.     ReDim x(1 To num_points)
  78.     ReDim y(1 To num_points)
  79. End Property
  80. ' ************************************************
  81. ' Compute the world coordinate bounds for the
  82. ' polygon.
  83. ' ************************************************
  84. Sub Bound(xmin As Single, ymin As Single, xmax As Single, ymax As Single)
  85. Dim i As Integer
  86.  
  87.     If num_points = 0 Then
  88.         xmin = 0
  89.         xmax = 0
  90.         ymin = 0
  91.         ymax = 0
  92.         Exit Sub
  93.     End If
  94.     
  95.     xmin = x(1)
  96.     xmax = x(1)
  97.     ymin = y(1)
  98.     ymax = y(1)
  99.     For i = 2 To num_points
  100.         If x(i) < xmin Then xmin = x(i)
  101.         If y(i) < ymin Then ymin = y(i)
  102.         If x(i) > xmax Then xmax = x(i)
  103.         If y(i) > ymax Then ymax = y(i)
  104.     Next i
  105. End Sub
  106.  
  107. ' ************************************************
  108. ' Write a polygon to a file using Write.
  109. ' Begin with "POLYGON" to identify this object.
  110. ' ************************************************
  111. Sub FileWrite(filenum As Integer)
  112. Dim i As Integer
  113.  
  114.     Write #filenum, "POLYGON", num_points
  115.     For i = 1 To num_points
  116.         Write #filenum, x(i), y(i)
  117.     Next i
  118. End Sub
  119. ' ************************************************
  120. ' Draw the polygon on a Form, Printer, or
  121. ' PictureBox.
  122. ' ************************************************
  123. Sub Draw(canvas As Object)
  124. Dim i As Integer
  125.  
  126.     ' Don't draw if there are no points.
  127.     If num_points < 1 Then Exit Sub
  128.        
  129.     canvas.CurrentX = x(1)
  130.     canvas.CurrentY = y(1)
  131.     For i = 2 To num_points
  132.         canvas.Line -(x(i), y(i))
  133.     Next i
  134. End Sub
  135.  
  136. ' ************************************************
  137. ' Read a polygon from a file using Input.
  138. ' Assume the "POLYGON" label has already been read.
  139. ' ************************************************
  140. Sub FileInput(filenum As Integer)
  141. Dim i As Integer
  142.  
  143.     Input #filenum, num_points
  144.     If num_points < 1 Then Exit Sub
  145.     ReDim x(1 To num_points)
  146.     ReDim y(1 To num_points)
  147.     For i = 1 To num_points
  148.         Input #filenum, x(i), y(i)
  149.     Next i
  150. End Sub
  151.  
  152.  
  153.